<?php

namespace App\Http\Controllers;

use Doctrine\DBAL\Schema\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


//引入模型
use App\Models\Article\Article;
use App\Models\Article\Comment;
use App\Models\Article\Type;

class HomeController extends Controller
{

    public function index()
    {
        //获取路由参数
        $typeid = request('typeid', 0);
        $keyword = request('keyword', '');

        $where = [];

        //随机查询6个分类
        $typelist = DB::table('article_type')->orderBy(DB::raw('RAND()'))->limit(6)->get();

        $type = Type::find($typeid);

        if($type)
        {
            $where['typeid'] = $typeid;
        }

        if(!empty($keyword))
        {
            $where[] = ['title', 'like', "%$keyword%"];
        }

        //关联查询
        $artlist = Article::with(['type'])->where($where)->orderBy('id', 'desc')->paginate(5);


        //搜索推荐文章
        $toplist = DB::table('article')->whereNotNull('thumb')->orderBy('visit', 'desc')->limit(5)->get();

        //赋值
        $data = compact([
            'artlist',
            'typelist',
            'toplist',
            'type'
        ]);

        return view('home.index', $data);
    }

    public function info()
    {
        $artid = request('artid', 0);

        //根据id查询数据
        $info = Article::with(['type'])->find($artid);

        //如果数据为空，就返回上一个界面
        if(!$info)
        {
            //没有数据的
            return redirect('/');
        }

        //更新访问量
        $visit = $info->visit ? $info->visit + 1 : 1;

        //更新数量
        Article::where(['id' => $artid])->update(['visit' => $visit]);

        //相关推荐 如果有分类id 就查询分类下面的文章，如果没有 就随机查询
        $artlist = [];
        if($info->typeid)
        {
            $artlist = Article::with(['type'])->where(['typeid' => $info->typeid])->orderBy('id', 'desc')->limit(10)->get();
        }else
        {
            $artlist = Article::orderBy(DB::raw('RAND()'))->limit(10)->get();
        }

        $comment = Comment::where(['artid' => $artid])->orderBy('created_at', 'asc')->paginate(10);

        //传递数据的方法
        $data = compact([
            'info',
            'artlist',
            'artid',
            'comment'
        ]);

        return view('home.info', $data);
    }

    //评论方法
    public function comment()
    {
        $artid = request('artid', 0);
        $content = request('content', '');

        //评论内容为空
        if(empty($content))
        {
            //没有数据的
            return redirect('/');
        }

        //根据id查询数据
        $info = Article::with(['type'])->find($artid);

        //如果数据为空，就返回上一个界面
        if(!$info)
        {
            //没有数据的
            return redirect('/');
        }

        //获取用户ip
        $addr = getIP();
        // $addr = "121.12.123.201";

        $address = null;

        if(!empty($addr) && $addr != "::1" && $addr != "127.0.0.1")
        {
            //调用接口根据ip地址来换取城市名称
            $url = "https://restapi.amap.com/v3/ip?key=fdb597223d0c9319d6ceb78c0b0657eb&ip=$addr&output=json";
            $result = file_get_contents($url);

            $json = json_decode($result, true);

            $province = isset($json['province']) ? $json['province'] : '';
            $city = isset($json['city']) ? $json['city'] : '';

            $address = "$province $city";
        }

        //插入数据库
        $comment = new Comment();
        $comment->artid = $artid;
        $comment->content = $content;
        $comment->address = $address;

        $result = $comment->save();

        if($result === FALSE)
        {
            return redirect()->route('error', ['message' => '评论失败']);
        }else
        {
            return redirect()->route('success', ['message' => '评论成功', 'url' => url('/info', ['artid' => $artid])]);
        }
    }

    //分类列表
    public function type()
    {
        //关联查询
        $typelist = Type::orderBy('id', 'asc')->paginate(40);

        //赋值数据
        $data = compact([
            'typelist'
        ]);

        return view('home.type', $data);
    }


    //消息提醒
    public function success()
    {
        $message = request('message', '');
        $url = request('url', '');

        $result = true;
        $data = compact([
            'message',
            'result',
            'url'
        ]);

        return view('home.notice', $data);
    }

    //消息提醒
    public function error()
    {
        $message = request('message', '未知消息');
        $url = request('url', '');

        $result = false;
        $data = compact([
            'message',
            'result',
            'url'
        ]);

        return view('home.notice', $data);
    }
}
